﻿using BNS_Education.IBLL;
using BNS_Education.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;


namespace BNS_Education.UI.Controllers
{
    public class QuestionBankController : Controller
    {
        private IaddExam BLLaddExam = BNS_Education.BLLContainer.Container.Resolve<IaddExam>();
        private IG_showTixing showTixing = BNS_Education.BLLContainer.Container.Resolve<IG_showTixing>();

        // GET: QuestionBank
        /// <summary>
        /// 题库首页显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {            
            List<question> tixing = showTixing.GetModels(t=>true).ToList();
            ViewBag.tx = new SelectList(tixing, "questionId", "questionName");
            List<topic> tiku = BLLaddExam.GetModels(t => t.topicId==1).ToList();
            return View(tiku);
        }
        /// <summary>
        /// 添加试题页面
        /// </summary>
        /// <returns></returns>
        public ActionResult addExam(int id = 1)
        {
            Session["tixing"] = id;
            if (id == 1)
            {
                ViewBag.prital = "Danxvanti";
            }
            else if (id == 2)
            {
                ViewBag.prital = "Duoxvanti";
            }
            else if (id == 3)
            {
                ViewBag.prital = "Duoxvanti";
            }
            else if (id == 4)
            {
                ViewBag.prital = "Tiankong";
            }
            else if (id == 5)
            {
                ViewBag.prital = "Panduan";
            }
            else if (id == 6)
            {
                ViewBag.prital = "Wenda";
            }
            else
            {
                return View();
            }
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]//对表单输入的内容不进行任何验证
        public ActionResult addExam1(FormCollection fom)
        {
            var xvanxiang = "";

            //题型
            int tixing = (int)Session["tixing"];
            //题干
            var text1 = fom["editor"];
            text1 = Regex.Replace(text1, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);//通过正则表达式过滤文本标签                                                                          //选项
            if (tixing == 1 || tixing == 2 || tixing == 3)
            {                      
                var dx1 = fom["dx1"];//选项一
                var dx2 = fom["dx2"]; //选项二
                var dx3 = fom["dx3"]; //选项三
                var dx4 = fom["dx4"]; //选项四
                xvanxiang = "dx1,dx2,dx3,dx4";
               
            }
            else if (tixing == 4 || tixing == 6)
            {
                xvanxiang = fom["tk"];
            }
            else
            {              
                xvanxiang = fom["rad"];
            }
            //难度
            var nandu = fom["nd"];
            //默认分值
            var fenzhi = fom["fz"];
            //题目解析
            var text2 = fom["jiexi"];
            //状态
            var ZT = 1;

            topic top = new topic();
            top.questionId =tixing.ToString();
            top.topicName = text1;
            top.topicNandu = nandu;
            top.topicXuanXiang = xvanxiang;
            top.topicFenZhi = fenzhi;
            top.topicJieXi = text2;
            top.topicZT = ZT;

            if (BLLaddExam.Add(top))
            {
                return Redirect("Index");
            }
            else
            {
                return Content("no");
            }
            
        }
        /// <summary>
        /// 修改题目状态 0：已删除  1：可使用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult delExam(int id)
        {

            var result = BLLaddExam.GetModels(t => t.topicId == id).FirstOrDefault();

            topic top = result;
            top.topicZT = 0;
            if (BLLaddExam.Update(top))
            {
                return Redirect("Index");
            }
            else
            {
                return Content("no");
            }
        }
        /// <summary>
        /// 根据Id 修改----页面展示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult showExamByid(int id)
        {           
            var result = BLLaddExam.GetModels(t =>true).ToList();
            var linq = result.Where(p => p.topicId == id);
            return View(linq);
        }
        [HttpPost]
        [ValidateInput(false)]//对表单输入的内容不进行任何验证
        public ActionResult updtExamByid(FormCollection fom)
        {
            
            return View();
        }
        /// <summary>
        /// 模态窗
        /// </summary>
        /// <returns></returns>
        public ActionResult Motai()
        {
            return View();
        }
        /// <summary>
        /// 批量导入页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Pdaoru()
        {
            return View();
        }
    }
}